package com.example.luobaomessage;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
TextView carprotected,btndianchi;
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        carprotected=findViewById(R.id.carprotected);
        btndianchi=findViewById(R.id.btndianchi);

if (Build.VERSION.SDK_INT>9){
    StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}
//
        Login();

        carprotected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Intent intent=new Intent(MainActivity.this,MainActivity2.class   );
               startActivity(intent);


            }
        });
        btndianchi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent=new Intent(MainActivity.this,MainActivity3.class   );
                startActivity(intent);

            }
        });


    }

    private void Login() {


        String url = "http://124.93.196.45:10001/dev-api/login";
        OkHttpClient client = new OkHttpClient();
        JSONObject object = new JSONObject();
        try {
            object.put("username", "16607699001");
            object.put("password", "123456");

        } catch (JSONException e) {
            e.printStackTrace();
        }


        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, String.valueOf(object));


        okhttp3.Request request = new Request.Builder().url(url).post(body).build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Toast.makeText(MainActivity.this, "login失败。。。。。。。。。。。。", Toast.LENGTH_SHORT).show();

                    }
                });


            }


            public void onResponse(Call call, Response response) throws IOException {

                String json = response.body().string();

                runOnUiThread(new Runnable() {
                    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
                    @Override
                    public void run() {


                        try {

                            JSONObject object1 = new JSONObject(json);

                            MyApp.token = object1.getString("token");
                            Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            String code = object1.getString("code");
                            if (code.equals("200")) {
                                YiBiaoPing();
                            }


                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }

                    }
                });


            }
        });

    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void YiBiaoPing() {
        DisplayManager displayManager = (DisplayManager) MainActivity.this.getSystemService(Context.DISPLAY_SERVICE);

        Display[] displays = displayManager.getDisplays();
        if (displays.length > 1) {

            Intent intent = new Intent(MainActivity.this, LuoBaoYiBiao.class);

            intent.setClassName(getPackageName(), getPackageName() + ".LuoBaoYiBiao");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

            ActivityOptions options = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                options = ActivityOptions.makeBasic();
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                options.setLaunchDisplayId(displays[1].getDisplayId());
            }
            startActivity(intent, options.toBundle());
        }
    }
}